package demo

import com.gitee.ixtf.lucene.Jlucene
import com.gitee.ixtf.lucene.kotlinx.result
import com.gitee.ixtf.lucene.kotlinx.text
import org.apache.lucene.index.DirectoryReader
import org.apache.lucene.index.IndexWriter
import org.apache.lucene.index.IndexWriterConfig
import org.apache.lucene.search.BooleanQuery
import org.apache.lucene.search.IndexSearcher
import org.apache.lucene.store.ByteBuffersDirectory
import org.lionsoul.jcseg.ISegment
import org.lionsoul.jcseg.analyzer.JcsegAnalyzer
import org.lionsoul.jcseg.dic.DictionaryFactory
import org.lionsoul.jcseg.segmenter.SegmenterConfig

private val analyzer by lazy {
  val config = SegmenterConfig(true)
  val dic = DictionaryFactory.createSingletonDictionary(config)
  JcsegAnalyzer(ISegment.MOST, config, dic)
}
private val indexWriter by lazy { IndexWriter(ByteBuffersDirectory(), IndexWriterConfig(analyzer)) }

fun main(args: Array<String>) {
  index()
  search()
}

fun search() {
  val indexReader = DirectoryReader.open(indexWriter)
  val searcher = IndexSearcher(indexReader)
  val result =
      BooleanQuery.Builder().run {
        //        string(ID, "id-1")
        //        keyword(ID, "id-1")
        //        string(ID, listOf("id-1", "id-2"))
        //        keyword(ID, "id-1", "id-2")
        text(analyzer, "text", "text")
        result(searcher, 0, 10)
      }
  println(result.count)
}

fun index() {
  for (idx in 1..10) {
    indexWriter.addDocument(Jlucene.doc("id-$idx").apply { text("text", "text-$idx") })
  }
  indexWriter.commit()
}
